//
//  HMTryReadTableViewController.m
//  HowToDo
//
//  Created by 宋轩 on 17/1/4.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMTryReadTableViewController.h"
#import "HMTryReadTableViewCell.h"
#import "HMTryReadViewModel.h"
#import "HMSpringFlowersPlayMP3Controller.h"

@interface HMTryReadTableViewController ()

@property (nonatomic, strong) NSArray *tryReadModel;

@end

static NSString *tryReadCellID = @"TryReadCellID";

@implementation HMTryReadTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadTryReadData];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HMTryReadTableViewCell" bundle:nil] forCellReuseIdentifier:tryReadCellID];
    
    self.tableView.estimatedRowHeight = 300;
    self.tableView.rowHeight = 300;

    [self.tableView addTableViewRefreshHeaderWhenPullToPerformAction:@selector(loadTryReadData)];

    self.navigationItem.title = @"试读";

    //-------------设置导航栏开始--------------------------------
    [super setupNavRightItemsByOne];
    //-------------设置导航栏结束--------------------------------
}

- (void)loadTryReadData
{
    [HMTryReadViewModel getTryReadModelWithID:self.tid Finished:^(NSArray *arr) {
        
        _tryReadModel = arr;
        
        [self.tableView reloadData];
        
        [self.tableView endRefresh];
        
    } Failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.tryReadModel.count;
}

//选中cell执行
-  (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMSpringFlowersPlayMP3Controller *vc = [[HMSpringFlowersPlayMP3Controller alloc] init];
    
    vc.urlStrID = [self.tryReadModel[indexPath.row] tryReadID];
    
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMTryReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tryReadCellID forIndexPath:indexPath];
    cell.tryReadModel = self.tryReadModel[indexPath.row];
    
//    self.navigationItem.title = cell.tryReadModel.item_title;
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
